<template>
    <Dialog v-model="dialogVisible" :title="dialogTitle" width="800">
        <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="80px">
            <el-form-item label="公告标题" prop="title">
                <el-input v-model="formData.title" placeholder="请输入公告标题"/>
            </el-form-item>
            <el-form-item label="公告内容" prop="content">
                <Editor v-model="formData.content" height="180px"/>
            </el-form-item>
            <el-form-item label="推送分类" prop="type">
                <el-select v-model="formData.type" clearable placeholder="请选择推送人员">
                    <el-option v-for="dict in getIntDictOptions(DICT_TYPE.YXT_NOTIFY_PUSH_TYPE)" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="formData.status" clearable placeholder="请选择状态">
                    <el-option v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
        </template>
    </Dialog>
</template>
<script lang="ts" setup>
import {DICT_TYPE, getIntDictOptions} from '@/utils/dict'
import {CommonStatusEnum} from '@/utils/constants'
import {NotifyVO, useNotifyApi} from "@/api/yxt/notify";
import {useMessage} from "@/components/Notice";

defineOptions({name: 'YxtNotifyForm'})

const {t} = useI18n()
const notifyApi = useNotifyApi();
const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref({
    id: undefined,
    title: '',
    type: 0,
    content: '',
    status: CommonStatusEnum.DISABLE,
})
const formRules = reactive({
    title: [{required: true, message: '公告标题不能为空', trigger: ['change', 'blur']}],
    type: [{required: true, message: '推送人员分类不能为空', trigger: ['change', 'blur']}],
    status: [{required: true, message: '状态不能为空', trigger: ['change', 'blur']}],
})
const formRef = ref()

const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    if (id) {
        formLoading.value = true
        try {
            formData.value = await notifyApi.getNotify(id)
        } finally {
            formLoading.value = false
        }
    }
}
defineExpose({open})

const emit = defineEmits(['success'])
const submitForm = () => {
    formRef.value?.validate(async (valid: any) => {
        if (valid) {
            formLoading.value = true
            try {
                const data = formData.value as unknown as NotifyVO
                if (formType.value === 'create') {
                    await notifyApi.createNotify(data)
                    useMessage(t('common.createSuccess'));
                } else {
                    await notifyApi.updateNotify(data)
                    useMessage(t('common.updateSuccess'));
                }
                dialogVisible.value = false
                emit('success')
            } finally {
                formLoading.value = false
            }
        }
    });
}

const resetForm = () => {
    formData.value = {
        id: undefined,
        title: '',
        type: 0,
        content: '',
        status: CommonStatusEnum.DISABLE,
    }
    formRef.value?.resetFields()
}
</script>
